﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using GTAuto.DAL.Entities;
using GTAuto.DAL.Enums;
using GTAuto.DAL.Extensions;

namespace GTAuto.DAL.DAL
{
    public class DALFirms : DALBase
    {
        public static DALFirms Singleton = new DALFirms();

        public GTFirm Add(GTFirm gtFirm)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {
                var newFirm = new Firm();


                newFirm.Active = gtFirm.Active;
                newFirm.Description = gtFirm.Description;
                newFirm.Logo = gtFirm.Logo;
                newFirm.Name = gtFirm.Name;
                newFirm.ShortName = gtFirm.ShortName;
                newFirm.Email = gtFirm.Email;
                newFirm.SiteUrl = gtFirm.SiteUrl;
                newFirm.Phone = gtFirm.Phone;
                
                db.Firms.InsertOnSubmit(newFirm);
                db.SubmitChanges();

                gtFirm.Id = newFirm.Id;
                return gtFirm;
            }
        }


        public GTFirm Update(GTFirm gtFirm)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {

                var newFirm = db.Firms.SingleOrDefault(x => x.Id == gtFirm.Id);

                newFirm.Active = gtFirm.Active;
                newFirm.Description = gtFirm.Description;
                newFirm.Logo = gtFirm.Logo;
                newFirm.Name = gtFirm.Name;
                newFirm.ShortName = gtFirm.ShortName;
                newFirm.Email = gtFirm.Email;
                newFirm.SiteUrl = gtFirm.SiteUrl;
                newFirm.Phone = gtFirm.Phone;
                db.SubmitChanges();
                return gtFirm;
            }
        }


        public GTFirm Get(int id)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {
                var brand = db.Firms.SingleOrDefault(x => x.Id == id);
                GTFirm gtBrand = new GTFirm();
                gtBrand.UpdateTo(brand);
                return gtBrand;
            }
        }

        public List<GTFirm> GetPaged(int page, int count, out int itemsCount)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {

                var brds =
                 from p in db.Firms
                 orderby p.Id
                 select p;


                var brands = brds.ToList();

                itemsCount = brands.Count;

                var ret = new List<GTFirm>();
                foreach (var brand in brands.Page(page, count))
                {
                    GTFirm gtBrand = new GTFirm();
                    gtBrand.UpdateTo(brand);
                    ret.Add(gtBrand);
                }
                return ret;
            }
        }



        public void Delete(int id)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {
                var singleOrDefault = db.Firms.SingleOrDefault(x => x.Id == id);

                if (singleOrDefault != null)
                {
                    db.Firms.DeleteOnSubmit(singleOrDefault);
                    db.SubmitChanges();
                }
            }
        }
        

        public bool IsInCategory(int firmId,int categoryId)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {
               var temp =  db.FirmsWithCategories.SingleOrDefault(x => x.CategoryId == categoryId && x.FirmId == firmId);
                return temp != null;
            }
        }


        public void DeleteInCategory(int firmId, int categoryId)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {
                var temp = db.FirmsWithCategories.SingleOrDefault(x => x.CategoryId == categoryId && x.FirmId == firmId);
                if(temp!=null)
                {
                    db.FirmsWithCategories.DeleteOnSubmit(temp);
                }
                db.SubmitChanges();
            }
        }

        public void AddToCategory(int firmId, int categoryId)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {
                var temp = db.FirmsWithCategories.SingleOrDefault(x => x.CategoryId == categoryId && x.FirmId == firmId);
                if (temp == null)
                {
                    FirmsWithCategory category = new FirmsWithCategory();
                    category.FirmId = firmId;
                    category.CategoryId = categoryId;

                    db.FirmsWithCategories.InsertOnSubmit(category);
                }
                db.SubmitChanges();
            }
        }


        public List<GTFirmCategory> FirmsCategory(int firmId)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {
                List<FirmsWithCategory> firmsWithCategories = db.FirmsWithCategories.Where(x => x.FirmId == firmId).ToList();


                List<GTFirmCategory> list = new List<GTFirmCategory>();
                foreach (var firmsWithCategory in firmsWithCategories)
                {
                   GTFirmCategory category = new GTFirmCategory();
                    category.UpdateTo(firmsWithCategory.FirmCategory);

                    list.Add(category);
                }

                return list;
            }
        }


        public List<GTFirm> FirmsInCategory(int categoryId)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {
                var ret =  db.FirmsInCategory(categoryId).ToList();

                var list = new List<GTFirm>();

                foreach (var gtFirm in ret)
                {
                    var newFirm = new GTFirm();

                    newFirm.Id = gtFirm.Id;
                    newFirm.Active = gtFirm.Active;
                    newFirm.Description = gtFirm.Description;
                    newFirm.Logo = gtFirm.Logo;
                    newFirm.Name = gtFirm.Name;
                    newFirm.ShortName = gtFirm.ShortName;
                  
                    list.Add(newFirm);
                }
                return list;
            }
        }
    }
}
